<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class weiboController extends Controller
{
    //显示添加微博页面
    public function getAdd()
    {
        return view('admins.article-add');
    }

    //执行微博添加
    public function postAdd(Request $request)
    {
        $id = session('id');


        if (empty($id))
        {
            return view('admins.login');
        }

        //判断提交的按钮
        if ($request -> btn == '发布') {
            $caogao = '1';
        } else {
            $caogao = '0';
        }

        //上传微博配图
        if($request->hasFile('img')) {
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //文件夹
            $dirname = './uploads/weibo/';
            //文件名
            $file = $filename .'.'. $suffix;
            //移动
            $request->file('img')->move($dirname,$file);
            //修改图片属性
            $image = trim($dirname.$file,'.');
        } else {
            $image = "";
        }

        //判断书否允许被评论 0是禁止评论
        $is_reply = !isset($request -> is_allow) ? '1' : $request -> is_allow;

        //微博内容
        $content = empty($request -> content) ? "" : $request -> content ;


        //发布时间
        $time = time();

        //判断关键字是否已经存在 , 如果不存在才会写入数据库
        $keywords = $request -> keywords;
        $res = DB::table('types') -> where('typeName','=',$keywords) -> first();

        //写入数据库
        //判断关键字 是否已经存在,存在则不写入types表,直接检索读取,不存在则写入并且再读取
        if (empty($res)) {
            DB::table('types') -> insert(['typeName' => $keywords]);
            $ress = DB::table('types') -> where('typeName',$keywords) -> get();
            $tid = $ress[0]['tid'];
            $pid = DB::table('posts') -> insertGetId(['uid' => $id, 'tid' => $tid, 'img' => $image, 'content' => $content, 'subTime' => $time, 'status' => $is_reply, 'caogao' => $caogao]);
            //写入images表
            DB::table('images') -> insert(['uid' => $id, 'tid' => $tid, 'image' => $image, 'pid' => $pid]);
            //热点微博数默认为1
            $n = 1;
        } else {
            $ress = DB::table('types') -> where('typeName',$keywords) -> get();
            $tid = $ress[0]['tid'];
            $pid = DB::table('posts') -> insertGetId(['uid' => $id, 'tid' => $tid,'content' => $content, 'img' => $image,'subTime' => $time, 'status' => $is_reply, 'caogao' => $caogao]);
            //写入images表
            DB::table('images') -> insert(['uid' => $id, 'tid' => $tid, 'image' => $image, 'pid' => $pid]);

            $num = DB::table('types')
                -> where('tid',$tid)
                -> select('count')
                -> first();
            //热点微博计数
            $n = $num['count'] + 1;
        }

        $count = DB::table('posts') -> count();
        //检测posts表是否为空
        if ($count < 0) {
            return 'hello ! 空空也 , 未检索到微博';
        }

        //写入热点微博计数
        DB::update("update types set count =$n where tid=$tid");

        $weibos = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> orderBy('pid','asc')
            -> paginate(3);

        return view('admins.article-list',['weibo' => $weibos, 'count' => $count]);
    }

    //获取并显示微博列表
    public function getList()
    {
        $count = DB::table('posts') -> count();

        if ($count < 0) {
            return 'hello ! 空空也 , 系统未发现微博,快去发布吧';
        }
        $weibos = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> orderBy('pid','asc')
            -> paginate(3);

        return view('admins.article-list',['weibo' => $weibos, 'count' => $count]);
    }

    //回收站显示
    public function getRecycle()
    {
        $count = DB::table('posts') -> where('recycle','=','0') -> count();
        if ($count < 0) {
            return 'hello ! 回收站空空也 , 未发检索到废弃微博';
        }
        $weibos = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','0')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> paginate(3);

        return view('admins.article-recycle',['weibo' => $weibos, 'count' => $count]);
    }

    /**
     * ajax执行微博放入回收站
     */
    public function postRecycle(Request $request)
    {
        $pid = $request -> pid;
        $res = DB::table('posts') -> where('pid',$pid) -> update(['recycle'=>'0']);
        if ($res >= 1) {
            echo "1";
        } else {
            echo "0";
        }
    }


    /**
     * ajax执行微博伪删除
     */
    public function postDel(Request $request)
    {
        $pid = $request -> pid;
        $res = DB::table('posts') -> where('pid',$pid) -> update(['delete'=>'0']);
        if ($res >= 1) {
            echo "1";
        } else {
            echo "0";
        }
    }

    /**
     * ajax执行微博还原
     */
    public function postBack(Request $request)
    {
        $pid = $request -> pid;
        $res = DB::table('posts') -> where('pid',$pid) -> update(['recycle'=>'1']);
        if ($res >= 1) {
            echo "1";
        } else {
            echo "0";
        }
    }

    /**
     * ajax执行微博草稿发布
     */
    public function postSend(Request $request)
    {
        $pid = $request -> pid;
        $res = DB::table('posts') -> where('pid',$pid) -> update(['caogao'=>'1']);
        if ($res >= 1) {
            echo "1";
        } else {
            echo "0";
        }
    }

    /**
     *  微博草稿箱显示
     */
    public function getCaogao()
    {
        $count = DB::table('posts') -> where('caogao','=','0') -> count();
        if ($count < 0) {
            return 'hello ! 草稿箱空空也 , 未检索到草稿微博';
        }
        $weibos = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('caogao','=','0')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> paginate(3);

        return view('admins.article-caogao',['weibo' => $weibos, 'count' => $count]);
    }

    //编辑微博
    public function getEdit($pid)
    {
        $res = DB::table('posts')
                    -> where('pid',$pid)
                    -> select('uid', 'content', 'subTime', 'tid', 'status')
                    -> first();
        $tid = $res['tid'];
        $type = DB::table('types') -> where('tid',$tid) -> select('typeName') ->first();
        $typeName = $type['typeName'];
        return view('admins.article-edit', ['weibo' => $res,'typeName'=>$typeName, 'pid'=>$pid]);
    }

    //提交执行修改微博数据
    public function postEdit(Request $request,$pid)
    {
        //判断内容
        $content = empty($request -> content) ? "" : $request -> content ;

        $time = time();

        //判断书否允许被评论 0是不允许评论
        $is_reply = !isset($request -> is_allow) ? '1' : $request -> is_allow;

        //写入更新数据库
        DB::table('posts')->where('pid',$pid) -> update(['content' => $content, 'subTime' => $time, 'status' => $is_reply]);

        return "修改成功";
    }

    /**
     * 微博搜索
     */
    public function postSearch(Request $request)
    {
        $keywords =  $request -> keywords;
        $count = DB::table('posts')
            -> where('content','like',"%$keywords%")
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> count();
        if ($count < 0) {
            return  redirect() -> back();
        }

        $weibos = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('content','like',"%$keywords%")
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> orderBy('pid','asc')
            -> paginate(3);

        return view('admins.article-list',['weibo' => $weibos, 'count' => $count]);

    }

    /**
     * 回收站搜索
     */
    public function postSearchrec(Request $request)
    {
        $keywords =  $request -> keywords;
        $count = DB::table('posts')
            -> where('content','like',"%$keywords%")
            -> where('recycle','=','0')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> count();
        if ($count < 0) {
            return  redirect() -> back();
        }

        $weibos = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('content','like',"%$keywords%")
            -> where('recycle','=','0')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> orderBy('pid','asc')
            -> paginate(3);

        return view('admins.article-recycle',['weibo' => $weibos, 'count' => $count]);

    }

    /**
     * 草稿箱搜索
     */
    public function postSearchcg(Request $request)
    {
        $keywords =  $request -> keywords;
        $count = DB::table('posts')
            -> where('content','like',"%$keywords%")
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','0')
            -> count();
        if ($count < 0) {
            return  redirect() -> back();
        }

        $weibos = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('content','like',"%$keywords%")
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','0')
            -> orderBy('pid','asc')
            -> paginate(3);

        return view('admins.article-recycle',['weibo' => $weibos, 'count' => $count]);

    }

}
